package QHgui.docing;

import GlobleService.SGlobalFunctionEx;
import QHHelper.SAbsolutePathHelper;
import QHgui.QHMainFrame;
import SingleService.SingleInstance;
import SingleService.SingleInstanceKey;
import com.javadocking.dockable.Dockable;
import com.javadocking.dockable.DockableState;
import com.javadocking.dockable.action.DefaultDockableStateAction;
import com.javadocking.dockable.action.DockableStateAction;
import com.javadocking.dockable.action.DockableStateActionFactory;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.File;

/**
 * <p> TODU </P>
 *
 * @author yeager
 * @Version V1.1.1.0
 * @date 2023/11/24 10:31
 */
public class SDefaultDockableStateActionFactory implements DockableStateActionFactory {
    public class DefaultDockableStateActionEx extends DefaultDockableStateAction
    {
        int state=0;
        Dockable dock=null;
        public DefaultDockableStateActionEx(Dockable dockable, int newDockableState)
        {
            super(dockable,newDockableState);
            state=newDockableState;
            dock=dockable;
        }

        /**
         * Constructs an action to put the dockable in the given state.
         *
         * @param	dockable			The state of this dockable will be changed by this action.
         * @param	newDockableState	The new state for the dockable. This is a constant defined by {@link com.javadocking.dockable.DockableState}.
         * @param 	name				The name and short description of the action.
         */
        public DefaultDockableStateActionEx(Dockable dockable, int newDockableState, String name)
        {
            super(dockable,newDockableState,name);
            state=newDockableState;
            dock=dockable;
        }

        /**
         *
         * @param	dockable			The state of this dockable will be changed by this action.
         * @param	newDockableState	The new state for the dockable. This is a constant defined by {@link com.javadocking.dockable.DockableState}.
         * @param 	name				The name and short description of the action.
         * @param 	icon				The icon of the action.
         */
        public DefaultDockableStateActionEx(Dockable dockable, int newDockableState, String name, Icon icon)
        {
            super(dockable,newDockableState,name,icon);
            state=newDockableState;
            dock=dockable;
        }
        public void actionPerformed(ActionEvent actionEvent)
        {
            super.actionPerformed(actionEvent);
            QHMainFrame mf=(QHMainFrame) SingleInstance.getInstance(SingleInstanceKey.MainFrame);
            if(mf==null)
                return;
            //	mf.getWorkingPanel().fierAfterDrawingDockableStateChage(dock, state);
        }
    }
    // Implementations of WindowStateActionFactory.

    /**
     * Creates a default window state action for the states: DockableState.CLOSED, DockableState.NORMAL, DockableState.MAXIMIZED, DockableState.MINIMIZED, and DockableState.EXTERNALIZED.
     *
     * @param	dockable			The dockable whose state will be changed by the created action.
     * @param	newDockableState	The action puts the dockable in this state. Should be DockableState.NORMAL, DockableState.MAXIMIZED, DockableState.MINIMIZED, DockableState.CLOSED, or DockableState.EXTERNALIZED.
     * @return						The action to change the state of the window in which the dockable is docked.
     * @throws	IllegalArgumentException	If the given window state is not DockableState.NORMAL, DockableState.MAXIMIZED, DockableState.MINIMIZED, DockableState.CLOSED, or DockableState.EXTERNALIZED.

     */
    public DockableStateAction createDockableStateAction(Dockable dockable, int newDockableState)
    {

        boolean enabled = (dockable.getPossibleStates() & newDockableState) != 0;
        Icon icon = null;
        DockableStateAction action = null;
        switch (newDockableState)
        {
            case DockableState.CLOSED:
                if (enabled)
                {
                    icon= SGlobalFunctionEx.getConsiderSkinImageIcon(SAbsolutePathHelper.getImagePath()
                            + File.separator+"img"+ File.separator+"close12.gif");
                }
                else
                {
                    icon=SGlobalFunctionEx.getConsiderSkinImageIcon(SAbsolutePathHelper.getImagePath()
                            + File.separator+"img"+File.separator+"closeDisabled12.gif");
                }
                action = new SCloseDockableStateAction(dockable,icon);
                break;
            case DockableState.NORMAL:
                if (enabled)
                {
                    icon = SGlobalFunctionEx.getConsiderSkinImageIcon("normal12.gif");
                }
                else
                {
                    icon = SGlobalFunctionEx.getConsiderSkinImageIcon("normalDisabled12.gif");
                }
                action = new DefaultDockableStateActionEx(dockable, DockableState.NORMAL, 	"Restore", icon);
                break;
            case DockableState.MAXIMIZED:
                if (enabled)
                {
                    icon = SGlobalFunctionEx.getConsiderSkinImageIcon("maximize12.gif");
                }
                else
                {
                    icon = SGlobalFunctionEx.getConsiderSkinImageIcon("maximizeDisabled12.gif");
                }
                action = new DefaultDockableStateActionEx(dockable, DockableState.MAXIMIZED, "Maximize", icon);
                break;
            case DockableState.MINIMIZED:
                if (enabled)
                {
                    icon = SGlobalFunctionEx.getConsiderSkinImageIcon("minimize12.gif");
                }
                else
                {
                    icon = SGlobalFunctionEx.getConsiderSkinImageIcon("minimizeDisabled12.gif");
                }
                action = new DefaultDockableStateActionEx(dockable, DockableState.MINIMIZED, "Minimize", icon);
                break;
            case DockableState.EXTERNALIZED:
                if (enabled)
                {
                    icon = SGlobalFunctionEx.getConsiderSkinImageIcon("externalize12.gif");
                }
                else
                {
                    icon = SGlobalFunctionEx.getConsiderSkinImageIcon("externalizeDisabled12.gif");
                }
                action = new DefaultDockableStateActionEx(dockable, DockableState.EXTERNALIZED, 	"Externalize", icon);
                break;
            default: throw new IllegalArgumentException("Cannot create a DockableStateAction for the state [" + newDockableState + "]");
        }
        action.setEnabled(enabled);
        return action;

    }

}
